var pathRegexp = require('path-to-regexp');
var Layer=module.exports=function(path, options, fn){
    if (!(this instanceof Layer)) {
        console.log('no Layer class')
        return new Layer(path, options, fn);
    }
    var opts = options || {};
    this.handle=fn;
    this.name = fn.name || '<anonymous>';
    this.params = undefined;
    this.path = undefined;
    this.regexp = pathRegexp(path, this.keys = [], opts);
}
Layer.prototype.handle_request=function(req,res,next){
    var fn = this.handle;     
    console.log('layer prototype handle_request fn.length',fn.length,fn.toString())
    //函数.length返回的是参数的个数
    if (fn.length > 3) {
        // not a standard request handler
        return next();
    }
    try {
        fn(req, res, next);
    } catch (err) {
        next(err);
    }
}
Layer.prototype.match = function match(path) {
    var match
  
    if (path != null) {
      // fast path non-ending match for / (any path matches)
      
  
      // match the path
      match = this.regexp.exec(path)
    }
  
    if (!match) {
      this.params = undefined;
      this.path = undefined;
      return false;
    }
    //console.log(match)
    // store values
    this.params = {};//设置layer params 
    this.path = match[0]
  
    var keys = this.keys;
    var params = this.params;
  
    for (var i = 1; i < match.length; i++) {
      var key = keys[i - 1];
      var prop = key.name;
      var val = decode_param(match[i])
  
      if (val !== undefined || !(hasOwnProperty.call(params, prop))) {
        params[prop] = val;
      }
    }
  
    return true;
  };